package com.incross.dawintest;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.incross.dawin.DawinAdActivity;
import com.incross.dawin.DawinConfig;
import com.incross.dawin.util.CommonUtils;
import com.incross.dawin.views.DawinAdPlayer;

public class DawinTestActivity extends Activity {

	public static final int TAB_TARGET_AD = 1000;
	public static final int TAB_REAL_AD = 1100;
	public static final int AD_ACTIVITY = 10000;
	
	private Context mContext;
	
	private TextView mTargetAdTab, mRealAdTab;
	
	private LinearLayout mRealAdLayout, mTargetAdLayout;
	
	private RadioGroup mServerTypeRadio;
	
	private int mCurrentTab = TAB_REAL_AD;
	
	private int mSelectedServer = DawinConfig.DAWIN_SERVER_TEST;
	
	// real ad needs
	private EditText mGcd, mMcd, mPcd, mScd, mCcd, mRepeat;
	private Button mRealAdExecuteButton, mShareKakaoButton, mShareKakaoStoryButton;
	
	// target ad needs
	private EditText mCampaignId, mGroupId;
	private Button mTargetAdExecuteButton;
	
	private String mXmlString;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_dawin_test);
		initViews();
		BroadCastTest test = new BroadCastTest(this);
	}
	
	private void initViews() {
		mTargetAdTab = (TextView) findViewById(R.id.tv_dawintest_targetad);
		mTargetAdTab.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switchTab(TAB_TARGET_AD);
			}
		});
		
		mRealAdTab = (TextView) findViewById(R.id.tv_dawintest_realad);
		
		mRealAdTab.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switchTab(TAB_REAL_AD);
			}
		});
		
		mRealAdLayout = (LinearLayout) findViewById(R.id.ll_tab_real);
		mTargetAdLayout = (LinearLayout) findViewById(R.id.ll_tab_target);
		
		// real ad layout init
		mGcd = (EditText) findViewById(R.id.et_realad_gcd);
		mMcd = (EditText) findViewById(R.id.et_realad_mcd);
		mPcd = (EditText) findViewById(R.id.et_realad_pcd);
		mScd = (EditText) findViewById(R.id.et_realad_scd);
		mCcd = (EditText) findViewById(R.id.et_realad_ccd);
		mRepeat = (EditText) findViewById(R.id.et_realad_repeat);
		
		mRealAdExecuteButton = (Button) findViewById(R.id.btn_realad_execute);
		
		mRealAdExecuteButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(mGcd.getText().length() == 0) {
					Toast.makeText(mContext, "Goods code가 비었다", Toast.LENGTH_SHORT).show();
				} else if(mMcd.getText().length() == 0) {
					Toast.makeText(mContext, "Media code가 비었다", Toast.LENGTH_SHORT).show();
				} else if(mPcd.getText().length() == 0) {
					Toast.makeText(mContext, "Page code가 비었다", Toast.LENGTH_SHORT).show();
				} else if(mScd.getText().length() == 0) {
					Toast.makeText(mContext, "Screen code가 비었다", Toast.LENGTH_SHORT).show();
				} else if(mCcd.getText().length() == 0) {
					Toast.makeText(mContext, "Category code가 비었다", Toast.LENGTH_SHORT).show();
				} else {
//					int mcd = Integer.parseInt(mMcd.getText().toString());
//					if(mcd < 82 || mcd > 142) {
//						Toast.makeText(mContext, "똑바로 해라...", Toast.LENGTH_SHORT).show();
//					} else {
//						
//					}
					showRealAdActivity(mGcd.getText().toString(), mMcd.getText().toString(), mPcd.getText().toString(), mScd.getText().toString(), mCcd.getText().toString(), mRepeat.getText().toString());
				}
			}
		});
		
		// target ad layout init
		mCampaignId = (EditText) findViewById(R.id.et_targetad_campaignid);
		mGroupId = (EditText) findViewById(R.id.et_targetad_groupid);
		
		mTargetAdExecuteButton = (Button) findViewById(R.id.btn_targetad_execute);
		mTargetAdExecuteButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mCampaignId.getText().length() == 0) {
					Toast.makeText(mContext, "Campaign id 비었습니다", Toast.LENGTH_SHORT).show();
				} else if(mGroupId.getText().length() == 0) {
					Toast.makeText(mContext, "Group id 비었습니다.", Toast.LENGTH_SHORT).show();
				} else {
					showTargetAdActivity(mCampaignId.getText().toString(), mGroupId.getText().toString());
				}
				
			}
		});
		
		mServerTypeRadio = (RadioGroup) findViewById(R.id.rg_server_choice);
		mServerTypeRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
					case R.id.rb_real:
						mSelectedServer = DawinConfig.DAWIN_SERVER_REAL;
						break;
						
					case R.id.rb_test:
						mSelectedServer = DawinConfig.DAWIN_SERVER_TEST;
						break;
				}
				
			}
		});
		
		mShareKakaoButton = (Button) findViewById(R.id.btn_share_kakao);
		mShareKakaoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					
					CommonUtils.shareKakao(mContext, CommonUtils.KAKAO_TALK, "[DAWIN]", "Dwin kakao share test", "http://cafe.daum.net/TROY");
				} catch(UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				CommonUtils.goKaKao(mContext);
			}
		});
		mShareKakaoStoryButton = (Button) findViewById(R.id.btn_share_kakao_story);
		mShareKakaoStoryButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					CommonUtils.shareKakao(mContext, CommonUtils.KAKAO_STORY, "[Dawin]", "다윈 카카오 테스트 메세지!!!", "http://cafe.daum.net/TROY");
				} catch(UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				/*CommonUtils.goStory(mContext);*/
			}
		});
		
	}
	
	public void showRealAdActivity(String gcd, String mcd, String pcd, String scd, String ccd, String repeat) {
//		Intent intent = new Intent(mContext, DawinAdActivity.class); // DawinAdActivity 호출용
		Intent intent = new Intent(mContext, AdActivity.class); // DawinAdPlayer호출용
		Bundle extras = new Bundle();
		extras.putInt("server_type", mSelectedServer);
		extras.putString("gcd", gcd);
		extras.putString("mcd", mcd);
		extras.putString("pcd", pcd);
		extras.putString("scd", scd);
		extras.putString("ccd", ccd);
		extras.putString("repeat", repeat);
		extras.putBoolean("realServer", false);
		intent.putExtras(extras);
		startActivityForResult(intent, AD_ACTIVITY);
	}
	
	public void showTargetAdActivity(String campaignId, String groupId) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(null)
		.setMessage("준비중이에용~~")
		.setPositiveButton("확인", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).show();
		
		// TODO : 두 값이 차 있는 경우 실행할 내용 입력.. 서버팀 작업 여부에 따라 실행가능
		Intent intent = new Intent(mContext, AdActivity.class);
		Bundle extras = new Bundle();
		extras.putInt("ad_type", TAB_TARGET_AD);
		extras.putString("campaignId", campaignId);
		extras.putString("groupId", groupId);
		intent.putExtras(extras);
		startActivityForResult(intent, AD_ACTIVITY);
	}
	
	
	
	private void switchTab(int which) {
		if(mCurrentTab == which) return;
		
		mCurrentTab = which;
		
		switch(which) {
			case TAB_TARGET_AD:
				mTargetAdTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_black_sel));
				mRealAdTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_black_nor));
				
				mRealAdLayout.setVisibility(View.GONE);
				mTargetAdLayout.setVisibility(View.VISIBLE);
				break;
				
			case TAB_REAL_AD:
				mTargetAdTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_black_nor));
				mRealAdTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_black_sel));
				mRealAdLayout.setVisibility(View.VISIBLE);
				mTargetAdLayout.setVisibility(View.GONE);
				break;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_dawin_test, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		
		if(itemId == R.id.menu_xml) {
			if(mXmlString != null) {
				Intent intent = new Intent(mContext, XmlActivity.class);
				intent.putExtra("xml", mXmlString);
				startActivity(intent);
			} else {
				Toast.makeText(mContext, "일단 광고를 한번 보셈", Toast.LENGTH_SHORT).show();
			}
			
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK && requestCode == AD_ACTIVITY) {
			if(data != null) {
				int rCode = data.getIntExtra(DawinAdActivity.KEY_RESULT_CODE, -1);
				mXmlString = data.getStringExtra("xml");
				if(rCode != -1) {
					switch(rCode) {
						case DawinAdPlayer.AD_FINISH_ACTION_COMPLETE:
							Toast.makeText(mContext, "재생완료 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_ACTION_SKIP:
							Toast.makeText(mContext, "스킵 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_SCREEN_CLICK:
							Toast.makeText(mContext, "스크린 클릭 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK:
							Toast.makeText(mContext, "액션버튼 클릭", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_CALL_ACTION:
							Toast.makeText(mContext, "전화액션 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_NO_APPLICABLE_AD:
							Toast.makeText(mContext, "해당 광고 없음 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_BACK_KEY_PRESSED:
							Toast.makeText(mContext, "Back key 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_AFTER_ACTION:
							Toast.makeText(mContext, "액션버튼 동작 후 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_BY_SNS_SHARE:
							Toast.makeText(mContext, "Sns share button 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_AFTER_SNS_SHARE:
							Toast.makeText(mContext, "Sns 동작 후 종료", Toast.LENGTH_SHORT).show();
							break;

						case DawinAdPlayer.AD_FINISH_BY_TICKER:
							Toast.makeText(mContext, "티커 종료", Toast.LENGTH_SHORT).show();
							break;

						case DawinAdPlayer.AD_FINISH_AFTER_TICKER:
							Toast.makeText(mContext, "티커 동작 후 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_OTHER_REASON:
							Toast.makeText(mContext, "기타 이유로 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_NETWORK_DISCONNECTED:
							Toast.makeText(mContext, "Network disconnect.. 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_AD_INFO_RECEIVE_FAIL:
							Toast.makeText(mContext, "광고정보 수신 실패 종료", Toast.LENGTH_SHORT).show();
							break;
							
						case DawinAdPlayer.AD_FINISH_OTHER_ERROR:
							Toast.makeText(mContext, "기타 오류로 종료", Toast.LENGTH_SHORT).show();
							break;
					}
				}
			}
			
		}
	}
	
	

}
